//
//  MJTabbarItem.m
//  新浪微博
//
//  Created by mj on 13-4-21.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MJTabbarItem.h"
@implementation MJTabbarItem

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // 0.设置背景
        //[self setBackgroundImage:[UIImage imageNamed:@"tabbar_slider.png"] forState:UIControlStateSelected];
        
        // 1.设置UIImageView
        // 不需要在用户长按的时候调整图片为灰色
        self.adjustsImageWhenHighlighted = NO;
        // 设置UIImageView的图片居中
        self.imageView.contentMode = UIViewContentModeCenter;
        
        // 2.设置UILabel
        // 设置文字居中
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        // 设置字体大小
        self.titleLabel.font = [UIFont systemFontOfSize:10];
    }
    return self;
}

// 去掉高亮效果
- (void)setHighlighted:(BOOL)highlighted{}

#pragma mark 设置标题和图标
- (void)setTitle:(NSString *)title icon:(NSString *)icon
{
    // 1.设置图片
    [self setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
     
   // [self setImage:[UIImage imageNamed:[icon fileNameAppendString:@"_highlighted"]] forState:UIControlStateSelected];
    
    // 2.设置文字
    [self setTitle:title forState:UIControlStateNormal];
}

#pragma mark - 覆盖父类的2个方法
#pragma mark 设置按钮标题的frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect {
    UIImage *image =  [self imageForState:UIControlStateNormal];
    CGFloat titleY = image.size.height - 3;
    CGFloat titleHeight = self.bounds.size.height - titleY;
    return CGRectMake(0, titleY, self.bounds.size.width,  titleHeight);
}
#pragma mark 设置按钮图片的frame
- (CGRect)imageRectForContentRect:(CGRect)contentRect {
    UIImage *image =  [self imageForState:UIControlStateNormal];
    return CGRectMake(0, 0, contentRect.size.width, image.size.height);
}
@end